//
//  YbrRecordFinalMusic.m
//  wifibox
//
//  Created by xinle on 15/8/24.
//  Copyright (c) 2015年 yinbaner. All rights reserved.
//

#import "YbrRecordFinalMusicDao.h"

@implementation YbrRecordFinalMusicDao


// 将直接上传的音频或者存为草稿的音频插入缓存表中
+(BOOL)insert:(YbrRecordItem *)item
{
    __block BOOL isOk;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"insert into record_final (recordName ,recordUrl, recordImgUrl,recordTime,albumName,albumLei,albumImgUrl,currentTime,playCount,favCount,isUpload,courseId,classId) VALUES (%@,%@,%@,%@,%@,%@,%@,%@,%i,%i,%i,%i,%i)", item.recordName ,item.recordUrl,item.recordImgUrl,item.recordTime,item.albumName,item.albumLei,item.albumImgUrl,item.currentTime,item.playCount,item.favCount,item.isUpload,item.courseId,item.classId];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;

}


// 取出所有存放在本地的上传录音文件
+(NSArray *)query
{
    __block NSArray *arr;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from record_final"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrRecordItem *item =   [[YbrRecordItem alloc] init];
            item._id            =   [rs intForColumn:@"_id"];
            item.recordName     =   [rs stringForColumn:@"recordName"];
            item.recordUrl      =   [rs stringForColumn:@"recordUrl"];
            item.recordImgUrl   =   [rs stringForColumn:@"recordImgUrl"];
            item.recordTime     =   [rs stringForColumn:@"recordTime"];
            item.albumName      =   [rs stringForColumn:@"albumName"];
            item.albumLei       =   [rs stringForColumn:@"albumLei"];
            item.albumImgUrl    =   [rs stringForColumn:@"albumImgUrl"];
            item.courseId       =   [rs intForColumn:@"courseId"];
            item.playCount      =   [rs intForColumn:@"playCount"];
            item.currentTime    =   [rs stringForColumn:@"currentTime"];
            item.favCount       =   [rs intForColumn:@"favCount"];
            item.isUpload       =   [rs intForColumn:@"isUpload"];
            item.classId        =   [rs intForColumn:@"classId"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;
    
}

+(BOOL)deletes:(YbrRecordItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from record_final where _id = %i",item._id];
    }];
    return isOk;
}


+(BOOL)deletesWithFileName:(YbrRecordItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from record_final where recordUrl = %@",item.recordUrl];
    }];
    return isOk;
}


+(BOOL)update:(YbrRecordItem *)item
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.修改数据
        rs = [db executeQueryWithFormat:@"update record_final set recordName=%@,albumName=%@,albumLei=%@ where _id=%i",item.recordName,item.albumName,item.albumLei,item._id];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
    
}


+(BOOL)updateWithUpload:(YbrRecordItem *)item
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.修改数据
        rs = [db executeQueryWithFormat:@"update record_final set isUpload=%d where _id=%i",item.isUpload,item._id];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
    
}


// 插入或更新音频信息
+(BOOL)updateWithAlbum:(YbrRecordItem *)item
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.修改数据
        rs = [db executeQueryWithFormat:@"update record_final set isUpload=%d,albumName=%@,albumLei=%@,albumImgUrl=%@,courseId=%i where _id=%i",item.isUpload,item.albumName,item.albumLei,item.albumImgUrl,item.courseId,item._id];
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
    
}

@end
